<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input id="name">
<button onclick="con()">连接</button>
<input id="send">
<button onclick="send()">发送</button>
<br>
<button onclick="ks()">开始</button>

<button onclick="zb()">准备</button>
<br>
<button onclick="g()">过</button>

<button onclick="xz()">下注</button>

<button onclick="gz()">跟注</button>

<button onclick="jz()">加注</button>

<button onclick="qp()">弃牌</button>
<br>
<button onclick="clear()">清除</button>
<div id="box"></div>
</body>
<script>

    function con() {

        let name = document.getElementById('name');
        var url = "ws://localhost:8080/ws/play?name=" + name.value;
        testWebSocket(url);
    }

    function zb() {
        let c = '{"type":6}'
        doSend(c)
    }

    function clear() {
        let box = document.getElementById('box');
        box.innerHTML = ''
    }

    function ks() {
        let c = '{"type":2}'
        doSend(c)
    }

    function g() {
        let c = '{"type":5,"params":0}'
        doSend(c)
    }

    function xz() {
        let c = '{"type":5,"params":1}'
        doSend(c)
    }

    function gz() {
        let c = '{"type":5,"params":2}'
        doSend(c)
    }

    function jz() {
        let c = '{"type":5,"params":3}'
        doSend(c)
    }

    function qp() {
        let c = '{"type":5,"params":5}'
        doSend(c)
    }

    function send() {
        let item = document.getElementById('send');
        doSend(item.value)
    }

    function testWebSocket(url) {
        websocket = new WebSocket(url);
        websocket.onopen = function (evt) {
            onOpen(evt)
        };
        websocket.onclose = function (evt) {
            onClose(evt)
        };
        websocket.onmessage = function (evt) {
            onMessage(evt)
        };
        websocket.onerror = function (evt) {
            onError(evt)
        };
    }

    function onOpen(evt) {
        console.log("CONNECTED");
    }

    function onClose(evt) {
        console.log(evt)
        console.log("DISCONNECTED");
    }

    function onMessage(evt) {
        console.log('msg:');
        console.log(evt.data);
        let box = document.getElementById('box');
        box.innerHTML += evt.data;
        box.innerHTML += '<br/>';
    }

    function onError(evt) {
        console.log('err');
        console.log(evt.data);
    }

    function doSend(message) {
        console.log("SENT: " + message);
        websocket.send(message);
    }

</script>
</html>